import { DesignerEngine } from './core/DesignerEngine'
import { DesignerState } from './core/DesignerState'
import { DESIGNER_EXTENSION_POINT } from './core/DesignerEngine'
import { DesignerCommand, StandardCommandInvoker } from './core/CommandSystem'
import { PropertyEditorRegistry } from './core/PropertyEditorRegistry'

export class DesignerRuntime {
  private static instance: DesignerEngine

  static getEngine(): DesignerEngine {
    if (!this.instance) {
      this.instance = this.createEngine()
    }
    return this.instance
  }

  private static createEngine(): DesignerEngine {
    const engine = new DesignerEngine()
    
    // 注册状态管理实现
    const stateManager = new DesignerState()
    engine.registerExtension(
      DESIGNER_EXTENSION_POINT.STATE_MANAGER,
      stateManager
    )

    // 初始化命令系统
    const commandInvoker = new StandardCommandInvoker()
    engine.registerExtension(
      DESIGNER_EXTENSION_POINT.COMMAND_EXECUTOR,
      {
        execute: (command: DesignerCommand) => commandInvoker.execute(command),
        undo: () => commandInvoker.undo(),
        redo: () => commandInvoker.redo(),
        onCommand: commandInvoker.commandExecuted$
      }
    )

    // 初始化属性编辑器注册表
    const propertyEditorRegistry = new PropertyEditorRegistry()
    engine.registerExtension(
      DESIGNER_EXTENSION_POINT.PROPERTY_EDITOR,
      propertyEditorRegistry
    )

    return engine
  }
}

// 提供全局访问点
export const useDesigner = () => DesignerRuntime.getEngine()